/*
 * @lc app=leetcode.cn id=2 lang=java
 *
 * [2] 两数相加
 */

// @lc code=start
/**
 * Definition for singly-linked list.
 * public class ListNode {
 *     int val;
 *     ListNode next;
 *     ListNode() {}
 *     ListNode(int val) { this.val = val; }
 *     ListNode(int val, ListNode next) { this.val = val; this.next = next; }
 * }
 */

class Solution {
    public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
        ListNode resultlist = new ListNode(0);
        ListNode cur = resultlist;
        while(true){
            if(l1==null && l2==null)break;
            if (l1 !=null) {cur.val += l1.val;l1 = l1.next;}
            if (l2 !=null) {cur.val += l2.val;l2 = l2.next;}

            if(cur.val >9 || l1 !=null || l2 !=null) cur.next = (cur.val>9) ? new ListNode(cur.val/10) : new ListNode(0)  ;
            
            cur.val %= 10;
            cur = cur.next;

            
        }
        
        
        return resultlist;
    }
}
// @lc code=end

